//
//  NextStepTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/21.
//

import UIKit

protocol NextStepTableViewCellDelegate {
    func textFieldDidBrandModel(brand: String, model: String)
    
    func selectNextStepAntion(selectInex: NSInteger)
}

class NextStepTableViewCell: UITableViewCell {
    var bgView: UIView!
    var borderLayer1 = CALayer()
    var brandView: UIView!
    var brandTextField: UITextField!
    var emptyBrandLab: UILabel!
    var modelView: UIView!
    var modelTextField: UITextField!
    var emptyModelLab: UILabel!
    var dateBtn: UIButton!
    var rightImage: UIImageView!
    var emptyYearLab: UILabel!
    var nextStepBtn: UIButton!
    
    var delegate :NextStepTableViewCellDelegate!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.clipsToBounds = true
        self.selectionStyle = .none
        self.initUI()
    }
    
    func initUI(){
        self.bgView = UIView(frame: CGRect(x: 14, y: 0, width: kScreenWidth - 28, height: 359))
        self.bgView.layer.cornerRadius = 8
        // strokeCode
        borderLayer1 = CALayer()
        borderLayer1.backgroundColor = UIColor(red: 1, green: 0.98, blue: 0.94, alpha: 1).cgColor
        self.bgView.layer.addSublayer(borderLayer1)
        // fillCode
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 1, green: 0.96, blue: 0.83, alpha: 1).cgColor, UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 1, y: 1, width: kScreenWidth - 28, height: 357)
        bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
        bgLayer1.endPoint = CGPoint(x: 1, y: 1)
        self.bgView.layer.addSublayer(bgLayer1)
        // shadowCode
        self.bgView.layer.shadowColor = UIColor(red: 0.96, green: 0.96, blue: 0.98, alpha: 0.8).cgColor
        self.bgView.layer.shadowOffset = CGSize(width: 0, height: 4)
        self.bgView.layer.shadowOpacity = 1
        self.bgView.layer.shadowRadius = 8
        self.contentView.addSubview(self.bgView)
        
        
        self.brandView = UIView(frame: CGRect(x: 14, y: 35, width: self.bgView.frame.size.width - 28, height: 46))
        self.brandView.backgroundColor = .white
        self.brandView.layer.cornerRadius = 4
        self.brandView.layer.masksToBounds = true
        self.bgView.addSubview(self.brandView)
        
        self.brandTextField = UITextField(frame: CGRect(x: 14, y: 0, width: self.brandView.frame.size.width - 28, height: 46))
        self.brandTextField.textAlignment = .center
        self.brandTextField.textColor = Theme.titleLabColor
        self.brandTextField.font = UIFont(name: Theme.PingFangSCRegular, size: 14)
        self.brandTextField.placeholder = MMLocalizedString("brands", comment: "品牌")
        self.brandTextField.delegate = self
        self.brandTextField.keyboardType = .default
        self.brandTextField.returnKeyType = .done
        self.brandView.addSubview(self.brandTextField)
        self.brandTextField.attributedPlaceholder = NSAttributedString.init(string:MMLocalizedString("brands", comment: "品牌"), attributes: [
            NSAttributedString.Key.foregroundColor: Theme.titleLabColor])
        
        
        self.emptyBrandLab = UILabel(frame: CGRect(x: 14, y: self.brandView.frame.maxY, width: self.bgView.frame.size.width - 28, height: 20))
        self.emptyBrandLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.emptyBrandLab.textColor = Theme.redColor
        self.bgView.addSubview(self.emptyBrandLab)
        
      
        
        
        self.modelView = UIView(frame: CGRect(x: 14, y: self.brandView.frame.maxY + 20, width: self.bgView.frame.size.width - 28, height: 46))
        self.modelView.backgroundColor = .white
        self.modelView.layer.cornerRadius = 4
        self.modelView.layer.masksToBounds = true
        self.bgView.addSubview(self.modelView)
        
        self.modelTextField = UITextField(frame: CGRect(x: 14, y: 0, width: self.brandView.frame.size.width - 28, height: 46))
        self.modelTextField.textAlignment = .center
        self.modelTextField.textColor = Theme.titleLabColor
        self.modelTextField.font = UIFont(name: Theme.PingFangSCRegular, size: 14)
        self.modelTextField.placeholder = MMLocalizedString("model", comment: "型号")
        self.modelTextField.delegate = self
        self.modelTextField.keyboardType = .default
        self.modelTextField.returnKeyType = .done
        self.modelView.addSubview(self.modelTextField)
        self.modelTextField.attributedPlaceholder = NSAttributedString.init(string:MMLocalizedString("model", comment: "型号"), attributes: [
            NSAttributedString.Key.foregroundColor: Theme.titleLabColor])
        
        
        self.emptyModelLab = UILabel(frame: CGRect(x: 14, y: self.modelView.frame.maxY, width: self.bgView.frame.size.width - 28, height: 20))
        self.emptyModelLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.emptyModelLab.textColor = Theme.redColor
        self.bgView.addSubview(self.emptyModelLab)
        
        
        self.dateBtn = UIButton(frame: CGRect(x: 14, y: self.modelView.frame.maxY + 20, width: self.brandView.frame.size.width - 28, height: 46))
        self.dateBtn.setTitle(MMLocalizedString("year_manufacture", comment: "制造年份"), for: .normal)
        self.dateBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.dateBtn.backgroundColor = .white
        self.dateBtn.layer.cornerRadius = 4
        self.dateBtn.layer.masksToBounds = true
        self.dateBtn.setTitleColor(Theme.titleLabColor, for: .normal)
        self.dateBtn.addTarget(self, action: #selector(dateBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.dateBtn)
        self.dateBtn.tag = 50
        
        self.rightImage = UIImageView(frame: CGRect(x: self.dateBtn.frame.size.width - 32, y: 14, width: 18, height: 18))
        self.rightImage.image = UIImage(named: "icon_car_year")
        self.dateBtn.addSubview(self.rightImage)
        
        
        self.emptyYearLab = UILabel(frame: CGRect(x: 14, y: self.dateBtn.frame.maxY, width: self.bgView.frame.size.width - 28, height: 20))
        self.emptyYearLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 11)
        self.emptyYearLab.textColor = Theme.redColor
        self.bgView.addSubview(self.emptyYearLab)
        
        
        
        self.nextStepBtn = UIButton(frame: CGRect(x: 14, y: self.dateBtn.frame.maxY + 60, width: self.bgView.frame.size.width - 28, height: 46))
        self.nextStepBtn.backgroundColor = Theme.mainColor
        self.nextStepBtn.setTitle(MMLocalizedString("next", comment: "下一步"), for: .normal)
        self.nextStepBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.nextStepBtn.setTitleColor(.white, for: .normal)
        self.nextStepBtn.layer.cornerRadius = 4
        self.nextStepBtn.layer.masksToBounds = true
        self.nextStepBtn.addTarget(self, action: #selector(nextStepBtnAntion), for: .touchUpInside)
        self.bgView.addSubview(self.nextStepBtn)
        self.nextStepBtn.tag = 51
        
        
        
        self.emptyBrandLab.text = MMLocalizedString("please_enter_vehicle_brand", comment: "请输入车辆品牌")
        self.emptyModelLab.text = MMLocalizedString("please_enter_vehicle_model_number", comment: "请输入车辆型号")
        self.emptyYearLab.text = MMLocalizedString("please_select_manufacture", comment: "请选择车辆制造年份")
        self.addLayout()
        
        
    }
    
    func addLayout(){
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(359)
            make.bottom.equalTo(0)
        }
        borderLayer1.frame = self.bgView.bounds
        
        self.brandView.snp.makeConstraints { make in
            make.top.equalTo(35)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
        }
        
        self.emptyBrandLab.snp.makeConstraints { make in
            make.top.equalTo(self.brandView.snp.bottom)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        
        self.modelView.snp.makeConstraints { make in
            make.top.equalTo(self.brandView.snp.bottom).offset(20)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
        }
        
        self.emptyModelLab.snp.makeConstraints { make in
            make.top.equalTo(self.modelView.snp.bottom)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        
        self.dateBtn.snp.makeConstraints { make in
            make.top.equalTo(self.modelView.snp.bottom).offset(20)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
        }
        
        self.emptyYearLab.snp.makeConstraints { make in
            make.top.equalTo(self.dateBtn.snp.bottom)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        
        self.rightImage.snp.makeConstraints { make in
            make.right.equalTo(-14)
            make.width.height.equalTo(18)
            make.centerY.equalTo(self.dateBtn.snp.centerY)
        }
        
        self.nextStepBtn.snp.makeConstraints { make in
            make.top.equalTo(self.dateBtn.snp.bottom).offset(50)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(46)
        }
    }
    
    
    
    //setModel
    func setModel(model: FreeQuoteModel){
        self.brandTextField.text = model.carBrand ?? ""
        self.modelTextField.text = model.carStyle ?? ""
        self.dateBtn.setTitle(model.carDate ?? MMLocalizedString("year_manufacture", comment: "制造年份"), for: .normal)
        self.emptyBrandLab.isHidden = false
        self.emptyModelLab.isHidden = false
        self.emptyYearLab.isHidden = false
        if model.emptyBrand == ""{
            self.emptyBrandLab.isHidden = true
        }
        
        if model.emptyModel == ""{
            self.emptyModelLab.isHidden = true
        }
        
        if model.emptyYear == ""{
            self.emptyYearLab.isHidden = true
        }
    }
    
    
    
    
    
    //MARK: - Antion
    @objc func dateBtnAntion(){
        self.delegate?.selectNextStepAntion(selectInex: self.dateBtn.tag)
        
    }
    
    @objc func nextStepBtnAntion(){
        self.delegate?.selectNextStepAntion(selectInex: self.nextStepBtn.tag)
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension NextStepTableViewCell: UITextFieldDelegate{
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "\n" {
            textField.resignFirstResponder()
        }
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.delegate.textFieldDidBrandModel(brand: self.brandTextField.text ?? "", model: self.modelTextField.text ?? "")
    }
}
